import { Component } from "react";
import React from "react";
import {
  Cell,
  Form,
  Button,
  InputNumber,
  Input,
  TextArea,
  Picker,
  Radio,
  DatePicker,
} from "@nutui/nutui-react-taro";
import styles from "./index.scss";
import { useModel } from "@/store";
import ComponentLayout from "@/components/ComponentLayout";
import { InsertPatInfo, addPatInfo, queryPatInfo ,deletePatInfo} from "@/services/patInfo";
import { useState } from "react";
import { useRef } from "react";
import Taro from "@tarojs/taro";
import Layout from "@/components/Layout";
// import { Empty } from '@nutui/nutui-react-taro';

let datePickerStartDate = new Date(); // 创建一个表示当前日期和时间的Date对象
datePickerStartDate.setFullYear(datePickerStartDate.getFullYear() - 100); // 将年份设置为100年前的年份

export default () => {
  const { patientList, curPatientInfo, chagnePatient ,getPatientList} = useModel("patient");
  const { user } = useModel("user");  
  
  const handDelete= (e)=>{
    deletePatInfo(e?.patientId).then(res=>{
      Taro.showToast({
        title: "删除成功",
        icon: "none",
      });
      getPatientList(user);
    })
  }

  return (
    <Layout title="切换就诊人">
    <view className={styles.changePatientWarp}>
      <view>
        {patientList?.map((x) => {
          return (
            <>
              {curPatientInfo?.patientId === x.patientId && (
                  <view className={styles.curpat}>当前就诊人</view>
                )}
                 <view
              className={styles.patCard}
              key={x.patientId}
              onClick={async () => {
                if (curPatientInfo?.patientId !== x.patientId) {
                  await chagnePatient({ queryType: "1", queryNo: x.patientId });
                  Taro.navigateBack();
                }
              }}
            >
              <view className={styles.cardItem}>
              患者姓名: {x.patientName}
                {curPatientInfo?.patientId !== x.patientId && (
                  <view className={styles.curpat} onClick={(e)=>{
                    e.stopPropagation();
                    handDelete(x)
                  }}>删除</view>
                )}
              </view>
              <view className={styles.cardItem}>患者编号: {x.patientId}</view>
              <view className={styles.cardItem}>身份证号: {x.idNo}</view>
            </view>
           </>
           
          );
        })}
      </view>
      <view>
        <Button
          block
          type="info"
          shape={null}
          // color="#1677ff"
          onClick={() => {
            Taro.navigateTo({ url: "/pages/addPatient/index" });
          }}
        >
          添加就诊人
        </Button>
      </view>
    </view>
    </Layout>
  );
};
